import $service from '../../../service';
const app = getApp()
let page = 1
Page({
  data: {
    isHis: $service.isHis,
    oneActive: 0,
    tab: '',
    orderList: [], //订单列表
    total: 0,
    noMore: false,
    hisTabList: [{
      name: '全部',
      index: ''
    }, {
      name: '待付款',
      index: 0
    },  {
      name: '已支付',
      index: 1
    }, {
      name: '待自提',
      index: 2
    }, {
      name: '已完成',
      index: 3
    }, {
      name: '已取消',
      index: -1
    },],
    tabList: [{
      name: '全部',
      index: ''
    }, {
      name: '待付款',
      index: 10
    }, {
      name: '待发货',
      index: 20
    }, {
      name: '待自提',
      index: 35
    }, {
      name: '待收货',
      index: 30
    }, {
      name: '已完成',
      index: 40
    }, {
      name: '已取消',
      index: 0
    },],
    toView: 'v',
  },
  onShow() {
    page = 1
    this.setData({
      noMore: false
    })
    this.data.oneActive === 0 ?  this.getOrderList() : this.getCflzHisPayMentInfoList()
  },
  taptab(e) {
    var index = e.currentTarget.dataset.index;
    page = 1
    this.setData({
      tab: index,
      noMore: false,
      toView: 'v' + index
    })
    this.data.oneActive === 0 ?  this.getOrderList() : this.getCflzHisPayMentInfoList()
  },
  oneSelect(e) {
    let {index} = e.currentTarget.dataset
    this.setData({
      noMore: false,
      oneActive: index,
      tab: '',
      orderList: [],
    })
    this.data.oneActive === 0 ?  this.getOrderList() : this.getCflzHisPayMentInfoList()
  },
  getOrderList() {
    let params = {
      "current": page,
      "size": 10,
      "type": this.data.tab
    }
    app.http.getOrderList(params).then(res => {
      let list = $service.deepClone(res.data.list)
      if (this.data.noMore) return
      if (this.data.orderList.length == res.data.total) {
        this.setData({
          noMore: true
        })
      }
      if (page != 1) {
        list = this.data.orderList.concat(list)
      }
      this.setData({
        orderList: list,
        total: res.data.total
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  onPullDownRefresh() {
    this.setData({
      noMore: false
    })
    page = 1
    this.data.oneActive === 0 ?  this.getOrderList() : this.getCflzHisPayMentInfoList()
    wx.stopPullDownRefresh();
  },
  onReachBottom() {
    if (!this.data.onMore) {
      page++
      this.data.oneActive === 0 ?  this.getOrderList() : this.getCflzHisPayMentInfoList()
    }
  },
  goOrderDetil: function (e) {
    console.log(e.currentTarget.dataset);
    let {
      indentNo,
      paymentmethod,
      id
    } = e.currentTarget.dataset
    console.log(indentNo, paymentmethod);
    // 廉江订单跳转
    if (this.data.isHis) {
      let preId = this.data.oneActive === 1 ? id : indentNo
      $service.openPage("../order_msg/order_msg?prescription_id=" +  preId + '&type=' + this.data.oneActive);
      return
    }
    if (paymentmethod == 1) {
      $service.openPage("../order_msg/order_msg?prescription_id=" + indentNo);
    } else {
      //医保跳转(珠海的才要)
      if ($service.hospitalType == 'zhuhai') {
        wx.navigateToMiniProgram({
          appId: 'wx55bd4c7d421e388d',
          path: 'pages/homeYaofang/homeYaofang',
          // extraData: {
          //   foo: 'bar'
          // },
          // envVersion: 'develop',
          success(res) {
            // 打开成功
          }
        })
      }
    }

  },
  // 退款
  Cancel_indent: function () {
    wx.showModal({
      content: "请联系客服，客服热线：020-32640557；待和客服确认后，退款会在一到两个工作日后按原路退回微信账号，请留意微信通知提醒！",
      showCancel: false,
      confirmText: '知道了',
      success(res) {
        if (res.confirm) {
          //不操作
        }
      }
    })
  },
  // 操作按钮
  handleBtn(e) {
    let {
      type,
      id,
      total
    } = e.currentTarget.dataset
    switch (type) {
      case 'pay': // 去支付
        this.wxDoPay(total, id)
        break;
      case 'refund': // 退款
        this.cancel_indent();
        break;
      case 'logistics': // 物流
        $service.openPage("../check_express/check_express?prescription_id=" + id)
        break;
      default:
        break;
    }
  },
  // 退款
  cancel_indent() {
    wx.showModal({
      content: "请联系客服，客服热线：0668-2900020；待和客服确认后，退款会在一到两个工作日后按原路退回微信账号，请留意微信通知提醒！",
      showCancel: false,
      confirmText: '知道了',
      success(res) {
        //
      }
    })
  },
  // 继续支付
  wxDoPay(money, orderId) {
    wx.login({
      success: (res) => {
        let params = {
          "code": res.code,
          "money": Number(money),
          "orderSn": orderId
        }
        app.http.initiatePayment(params).then(res => {
          let {
            timeStamp,
            nonceStr,
            signType,
            paySign,
            packageWx
          } = res.data;
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package: packageWx,
            signType,
            paySign,
            success: (res) => {
              this.getOrderList()
              $service.showToast('支付成功')
            },
          })
        }).catch(err => {
          $service.showToast(err.msg)
        })
      }
    })
  },
  //跳转到小程序
  toOrderWx() {
    //医保跳转(珠海的才要)
    wx.navigateToMiniProgram({
      appId: 'wx55bd4c7d421e388d',
      path: 'pages/homeYaofang/homeYaofang',
      // extraData: {
      //   foo: 'bar'
      // },
      // envVersion: 'develop',
      success(res) {
        // 打开成功
      }
    })
  },
  // 廉江his订单列表
  getCflzHisPayMentInfoList() {
    app.http.getCflzHisPayMentInfoList({
      "current": page,
      "size": 10,
      "type": this.data.tab
    }).then(res => {
      let list = $service.deepClone(res.data.list)
      if (this.data.noMore) return
      if (this.data.orderList.length == res.data.total) {
        this.setData({
          noMore: true
        })
      }
      if (page != 1) {
        list = this.data.orderList.concat(list)
      }
      list.forEach(item => {
        item.create_time = item.createTime
        item.indent_no = item.outTradeNo
        item.indent_state = Number(item.payState) 
      })
      this.setData({
        orderList: list,
        total: res.data.total
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  }
})